package ASSET.RESOURCE;

import javax.swing.JTable;

import TOOLS.SQLtool;
import TOOLS.messageTool;

public class c_resourceModel {
	SQLtool sql;
	messageTool mt=new messageTool();
	
	public c_resourceModel(SQLtool iSql) throws Exception {
		sql=iSql;
	}
	
	public void newModel(String iName,String iKeyWords,String iType,int iMax,int iLife) throws Exception {
		sql.Command("insert resource_model (name,keyWords,type,maxUsage,lifeTime) values ('"+iName+"','"+iKeyWords+"','"+iType
				+"',"+iMax+","+iLife+")");
	}
	
	public void deleteModel(int iResourceModelIndex) throws Exception {
		sql.Command("delete from resource_model where resourceModelIndex="+iResourceModelIndex);
	}
	
	public void editModel(int iResourceModelIndex,String iName,String iKeyWords,String iType,int iMax,int iLife) throws Exception {
		sql.Update("update resource_model set name='"+iName+"',keyWords='"+iKeyWords+"',type='"+iType+"',maxUsage="+iMax
				+",lifeTime="+iLife+" where resourceModelIndex="+iResourceModelIndex);
	}

	public m_resourceModel getModel(int iResourceModelIndex) throws Exception {
		JTable t=sql.select("select resourceModelIndex,name,keyWords,type,maxUsage,lifeTime "
				+ "from resource_model where resourceModelIndex="+iResourceModelIndex);
		if(t.getRowCount()>0) {
			m_resourceModel result=new m_resourceModel();
			result.resourceModelIndex=Integer.parseInt((String)t.getValueAt(0, 0));
			result.name=(String)t.getValueAt(0, 1);
			result.keyWords=(String)t.getValueAt(0, 2);
			result.type=(String)t.getValueAt(0, 3);
			result.maxUsage=Integer.parseInt((String)t.getValueAt(0, 4));
			result.lifeTime=Integer.parseInt((String)t.getValueAt(0, 5));
			return result;
		}
		return null;
	}
	
	public m_resourceModel getModel(String iResourceModelName) throws Exception {
		JTable t=sql.select("select resourceModelIndex,name,keyWords,type,maxUsage,lifeTime "
				+ "from resource_model where name='"+iResourceModelName+"'");
		if(t.getRowCount()>0) {
			m_resourceModel result=new m_resourceModel();
			result.resourceModelIndex=Integer.parseInt((String)t.getValueAt(0, 0));
			result.name=(String)t.getValueAt(0, 1);
			result.keyWords=(String)t.getValueAt(0, 2);
			result.type=(String)t.getValueAt(0, 3);
			result.maxUsage=Integer.parseInt((String)t.getValueAt(0, 4));
			result.lifeTime=Integer.parseInt((String)t.getValueAt(0, 5));
			return result;
		}else {
			throw new Exception("-128");
		}
	}
	
}
